<?php
ob_start();
include("/var/www/html/new/d/common/header.html");
include("/var/www/html/new/d/common/left.html");
include("/var/www/html/new/d/common/page.php");
$out_html=ob_get_contents();
ob_end_clean();
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:20;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	$offset=0;
    $pageSize=9999;
}
$where = "aid={$_SESSION['sid']} AND refund_money>0";
if ($_REQUEST['ordernum']) {
    $where .= " AND ordernum='".trim($_REQUEST['ordernum'])."'";
}

$sqlTotal = "SELECT COUNT(*) AS cnt FROM pft_order_refund WHERE $where";
$GLOBALS['le']->query($sqlTotal);
$GLOBALS['le']->fetch_assoc();
$total = $GLOBALS['le']->f('cnt');

$sql = <<<SQL
SELECT id, ordernum, refund_num, refund_money, refund_fee,sourceT,
refund_time, handler_time, refund_status, batch_no, trade_no, subject
FROM pft_order_refund WHERE $where ORDER BY refund_time DESC,refund_status ASC LIMIT $offset,$pageSize
SQL;
$GLOBALS['le']->query($sql);
while($tmp =$GLOBALS['le']->fetch_assoc()) {
    $r[] = $tmp;
}
$dstatusT = array(
    '<span style="color:#F00;">未处理</span>',
    '<span style="color:#2d1eff;">已处理</span>',
);
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{   
	$filename=date('YmdHis').'退款列表';
	$Excel[0]['a']='订单号';
	$Excel[0]['b']='退款金额';
	$Excel[0]['c']='手续费';
	$Excel[0]['d']='原因';
	$Excel[0]['e']='申请时间';
	$Excel[0]['f']='操作时间';
	$Excel[0]['g']='状态';
	$i=2;
	foreach($r as $lid=>$row){
		$Excel[$n]['a']=$row['ordernum'];
        $Excel[$n]['b']=$row['refund_money']/100;
        $Excel[$n]['c']=$row['refund_fee']/100;
        $Excel[$n]['d']=$row['subject'];
        $Excel[$n]['e']=$row['refund_time'];
        $Excel[$n]['f']=$row['handler_time'];
        $Excel[$n]['g']= ($row['refund_status']==0)?'未处理':'已处理';
        $n++;
	}
	$xls = new SimpleExcel('UTF-8', true,'refund');
    $xls->addArray($Excel);
    $xls->generateXML($filename);
	exit;
}
echo $out_html;
?>
<link rel="stylesheet" href="css/page.css" media="all" />
<style type="text/css">
	form.search{/* border-bottom:1px solid #eee;*/font-size: 14px; margin:15px auto; padding:5px;}
	form.search input.ltitle{ width: 214px;height: 30px;background-color: #f5f5f5;border: 1px solid #e5e5e5;padding: 0px 3px;}
	form.search input.lbtn{ width: 100px;height: 32px;margin-top: 10px;line-height: 32px;border: 0;margin-left: -5px;background-color: #00a0e9;font-weight: normal;color: #FFF;cursor:pointer; font-size:13px;}
	table.ter{ text-align:center; font-size:14px; color:#666; width:100%; border:1px solid #E5E3E3;}
	table.ter caption{ font-size:16px; font-weight:700; color:#000; text-align:center;}
	table.ter tr{ height:30px;}
	table.ter th,table td{font-size:12px;}
	table.ter th{ background:url('images/tabshutian.png') left center #F3F3F3 no-repeat; font-weight:normal;text-align:center;}
	table.ter th.fir{ background:#F3F3F3;}
	table.ter td{ color:#111; border-top:1px solid #E5E3E3;}
	table.ter .oper{ cursor:pointer}
	.export {display: inline-block;margin-top: 10px;float: right;height: 32px;cursor: pointer;font-size: 0;
	border: 0 none;text-indent: -9999px;background: url(http://www.12301.cc/images/order/ysearchBtn.gif) no-repeat;}
	.exportbtn {width: 84px;background-position: right top;}
</style>
<script type="text/javascript" src="http://www.16u.cc/js/r.js"></script>
<script src="http://www.16u.cc/d/js/popBox.js"></script>
<div class="r_content">
	 <h3 class="mc_title">
		<span class="mctit_1"><a href="terchange.html"><span class="mctit_2">支付宝退款申请</span></a></span>
	</h3>
	<form class="search" >
        <input type="hidden" name="act" id="act"/>
		<label>订单号：</label><input type="text" name="ordernum" value="<?=$ordernum?>" class="ltitle"/>
		<input type="submit" value="查 询" class="lbtn"/>
		<input type="button" class="export exportbtn" value="导出" id="excel">
	</form>
	
	<div class="tabCon">
		<table class="ui-table ter">
			<tr>
                <th>编号</th>
				<th class="fir">订单号</th>
                <th>退款金额</th>
                <th>手续费</th>
				<th>原因</th>
				<th>申请时间</th>
				<th>操作时间</th>
				<th>支付渠道</th>
				<th>状态</th>
				<th>操作</th>
			</tr>
            <tbody>
            <form id="pay_frm" action="http://pay.12301.cc/refund_api.php" target="_blank" method="post">
                <input name="from" type="hidden" value="<?=$_SESSION['memberID']?>"/>
            <?php if(count($r)):?>
                <?foreach($r as $row):?>
                <tr>
                    <td class="chk">
                        <?=$row['id']?>
                        <input class="tids"
                               type="checkbox"
                            <?=$row['sourceT']==1 || $row['refund_status']==1 ? 'disabled' :''?>
                               name="log_id[]"
                               value="<?=$row['id']?>"/>
                    </td>
                    <td title="<?=$row['fxid']?>">
                        <a target="_blank" href="orderView.html?ordernum=<?=$row['ordernum']?>"><?=$row['ordernum']?></a>
                    </td>
                    <td><?=$row['refund_money']/100?></td>
                    <td><?=$row['refund_fee']/100?></td>
                    <td><?=$row['subject']?></td>
                    <td><?=$row['refund_time']?></td>
                    <td><?=$row['handler_time']?></td>
                    <td><?=$row['sourceT']==0 ? '支付宝' : '微信支付'?></td>
                    <td><?=$dstatusT[$row['refund_status']]?></td>
                    <td>
                        <?php if($row['refund_status']==0 && $row['sourceT']==0):?>
                            <a class="oper" href="javascript:;" data-ordernum="<?=$row['ordernum']?>">退款</a>
                        <?php elseif($row['refund_status']==0 && $row['sourceT']==1):?>
                            <a class="refundManual" href="javascript:;"
                               data-id="<?=$row['id']?>"
                               data-ordernum="<?=$row['ordernum']?>">退款</a>
                        <?php else:?>
                            已处理
                        <?php endif;?>
                    </td>
                </tr>
                <?endforeach;?>
            </tbody>
            <tfoot>
            <tr>
                <td>
                    <a class="ckall" href="javascript:;">全选</a>
                    <a class="reck" href="javascript:;">反选</a>
                </td>
                <td colspan="9" style="text-align: center;">
                    <input type="submit" value="批量退款"/>
                </td>
            </tr>
            </tfoot>
            <?php endif;?>
            </form>
		</table>
	</div>
	<?page($total,$currentPage,$pageSize);?>
</div>
    <script src="http://www.12301.cc/js/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function(){
    $(".oper").click(function(){
        $('input.tids').removeAttr('checked');
        $(this).parent().siblings('td.chk').find('input.tids').attr('checked', 'checked');
        $("#pay_frm").submit();
    });
    $(".ckall").click(function(){ // 全选
        $(".tids").attr("checked",true).parent();
    });
    $(".reck").click(function(){// 反选
        $(".tids").each(function(index,element){
            $(this).attr("checked",!this.checked).parent();
        });
    });
    $("#excel").click(function(){
        $("#act").val("Export");
        $(".search").submit();
    });

    $(".ui-table").on('click', 'a.refundManual', function(){
        var obj = $(this);
        if (obj.hasClass('ing')) {
            alert('处理中，别急...');
            return false;
        } else {
            obj.addClass('ing');
        }
        var alert_msg = '您确定要执行退款操作吗？';
//            +'\n微信退款接口出现故障，票付通平台还未对接；'
//            +'\n退款需要登陆到微信支付平台手动操作,执行此操作前请确保您已在微信后台提交退款申请。';
        if (!confirm(alert_msg)) {
            obj.removeClass('ing')
            return false;
        }
        var param = {
            action:'refundManual',
            log_id : $(this).data('id'),
            order_num:$(this).data('ordernum')
        };

        $.post('admin/wx_refund.php',param, function(json){
            if (json.code=='200') {
                obj.parent().text('');
                $("#log_status_"+param['log_id']).html('<span style="color:#2d1eff;">已处理</span>');
            } else {
                alert(json.msg);
            }
        },'json');
    });


});
</script>
<?php include("common/footer.html");?>